<?php
/**
 * 用户订单
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/11/27
 * Time: 下午6:42
 */
namespace app\common\model;

use think\Model;

class OrderModel extends Model
{
    const SHOW_STATUS=1; //正常
    const HIDE_STATUS=0; //禁用
    const DEL_STATUS=-1; //删除

    const TYPE_NO=0; //未服务
    const TYPE_OK=1; //已服务完成

    protected $autoWriteTimestamp = true;

    public function user()
    {
        return $this->hasOne('MemberModel','uid','user_id')->field('uid,mobile');
    }

    public function service()
    {
        return $this->hasOne('CarServiceModel','id','service_id')->field('id,title');
    }

    /**
     * 所有订单
     * @param array $where
     * @param int $pageSum
     * @return \think\Paginator
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getALL($where=[],$pageSum=0)
    {
        if($pageSum<=0){
            $pageSum=getSystem("pageSUM")?getSystem("pageSUM"):10;
        }

        $list=self::with('user')
            ->with('service')
            ->where('status','>',self::DEL_STATUS)
            ->where($where)
            ->order('status desc,order_type asc')
            ->paginate($pageSum);

        return $list;
    }

    /**
     * 获取导出数据
     * @param array $where
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getSelect($where=[]){
        $list=self::with('user')
            ->with('service')
            ->where('status','>',self::DEL_STATUS)
            ->where($where)
            ->order('status desc,order_type asc')
            ->select();

        foreach ($list as $vo){
            $brand=CarBrandModel::field('brand')->where('brand_id',$vo->brand_id)->find();
            $series=CarSeriesModel::field('series_name')->where('series_id',$vo->series_id)->find();
            $car=CarModel::field('car_name')->where('car_id',$vo->car_id)->find();

            if(!empty($brand)) $vo->brand_name=$brand->brand;
            if(!empty($series)) $vo->series_name=$series->series_name;
            if(!empty($car)) $vo->car_name=$car->car_name;
        }

        return $list;
    }

    /**
     * 删除
     * @param $id
     * @return array
     */
    public function del($id){

        $au=self::get($id);
        $au->status=self::DEL_STATUS;

        $status=$au->save();
        if($status){
            return ['status'=>1,'msg'=>'删除成功！'];
        }else{
            return ['status'=>0,'msg'=>'删除失败！'];
        }
    }

    public function getOrder($where=[],$pageSum=0)
    {
        if($pageSum<=0){
            $pageSum=getSystem("pageSUM")?getSystem("pageSUM"):10;
        }

        $list=self::with('user')
            ->with('service')
            ->where($where)
            ->order('create_time desc')
            ->paginate($pageSum);

        return $list;
    }
}